package com.bluemsun.naturallanguageprocessing.service.impl;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.service.IFileService;
import com.bluemsun.naturallanguageprocessing.util.FTPUtil;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Service("iFileService")
public class FileServiceImpl implements IFileService {
    private static Logger logger = LoggerFactory.getLogger(com.bluemsun.naturallanguageprocessing.service.impl.FileServiceImpl.class);
    @Autowired FTPUtil ftpUtil;
    public String upload(MultipartFile file, String path){

        String fileName = file.getOriginalFilename();
        String fileExtensionName = fileName.substring(fileName.lastIndexOf(".")+1);
        String uploadFileName = UUID.randomUUID().toString()+"."+fileExtensionName;

        logger.info("开始上传文件,上传文件的文件名:{},上传的路径:{},新文件名:{}",fileName,path,uploadFileName);

        File fileDir = new File(path);
        if(!fileDir.exists()){
            fileDir.setWritable(true);
            fileDir.mkdirs();
        }
        File targetFile = new File(path,uploadFileName);
        try {
            file.transferTo(targetFile);
            //文件已经上传成功了
            ftpUtil.uploadFile(Lists.newArrayList(targetFile));
            targetFile.delete();
        } catch (IOException e) {
            System.out.println(e);
            logger.error("上传文件异常",e);
            return null;
        }
        return targetFile.getName();
    }

     public ServerResponse Fileupload(MultipartFile[]files, HttpServletRequest request){
         List<String> list = new ArrayList<String>();
         if (files != null && files.length > 0) {
             for (int i = 0; i < files.length; i++) {
                 MultipartFile file = files[i];
                 // 保存文件
                 list = saveFile(request, file, list);
             }
         }
         return ServerResponse.createBySuccess("多图上传成功",list);
     }
    private List<String> saveFile(HttpServletRequest request,
                                  MultipartFile file, List<String> list) {

        if (!file.isEmpty()) {
            try {
//
                String fileName = file.getOriginalFilename();
                String fileExtensionName = fileName.substring(fileName.lastIndexOf(".")+1);
                String uploadFileName = UUID.randomUUID().toString()+"."+fileExtensionName;
                String filePath = request.getSession().getServletContext()
                        .getRealPath("/")
                        + "upload/" + uploadFileName;
                File fileDir = new File(filePath);
                logger.info("开始上传文件,上传文件的文件名:{},上传的路径:{},新文件名:{}",fileName,filePath,uploadFileName);
                if(!fileDir.exists()){
                    fileDir.setWritable(true);
                    fileDir.mkdirs();
                }
                // 转存文件
                File targetFile = new File(filePath,uploadFileName);
                try {
                    file.transferTo(targetFile);
                    list.add(filePath);
                    //文件已经上传成功了
                } catch (IOException e) {
                    logger.error("上传文件异常",e);
                    return null;
                }
                return list;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
